﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using OgilvyOne.DALFactory;

namespace OgilvyOne.BLL
{
    public class Login
    {
        public const string LOGIN_USER_KEY = "LOGIN_USER_KEY";
        public const string LOGIN_USER_POPEDOM_KEY = "LOGIN_USER_POPEDOM_KEY";

        public bool login(string username, string password, System.Web.SessionState.HttpSessionState session)
        {
            UsersModel user = LoginFactory.create().login(username, password);

            if (user != null)
            {
                session.Add(LOGIN_USER_KEY, user);
                if (isDynamicPopedom(session))
                {//设置用户权限
                    Hashtable map = RolePopedomFactory.create().getUserPopedomMenu(user.Userrole);
                    session.Add(LOGIN_USER_POPEDOM_KEY, map);
                }
                return true;
            }

            return false;

        }
        /// <summary>
        /// 是否权限设置或超级管理员
        /// </summary>
        /// <returns></returns>
        public bool isDynamicPopedom(System.Web.SessionState.HttpSessionState session)
        {
            string dp = OgilvyOne.Utility.ConfigUtil.GetStringValue("DynamicPopedom");
            if (dp == "1") return true;
            UsersModel user = (UsersModel)session[LOGIN_USER_KEY];
            if (("," + user.Userrole + ",").IndexOf(",1,") > -1)
            {
                return true;
            }
            return false;
        }

        public IList getColumnList(string type)
        {
            if (type == "systree")
            {
                return LoginFactory.create().getSysColumnList();
            }
            else
            {
                return LoginFactory.create().getColumnList();
            }
        }


    }
}
